﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;
using Hxsoft.AppV1.WebUI;

namespace Hxsoft.AppV1._Controls
{
	public partial class ModuleTreeDropDownList : System.Web.UI.UserControl
	{
		private LoadedValue<int?> loadedSiteId;
		public int? SiteId
		{
			get
			{
				if (loadedSiteId.Loader == null)
					loadedSiteId.Loader = () => Sites.CurrentSiteId;
				return loadedSiteId.Value;
			}
		}

		private LoadedValue<string> loadedModuleCode;
		public virtual string ModuleCode
		{
			get
			{
				if (loadedModuleCode.Loader == null)
					loadedModuleCode.Loader = () => (string)ViewState["ModuleCode"];
				return loadedModuleCode.Value;
			}
			set
			{
				if (loadedModuleCode.Setter == null)
					loadedModuleCode.Setter = v => ViewState["ModuleCode"] = v;
				loadedModuleCode.Value = value;
			}
		}

		private LoadedValue<string> loadedControlName;
		public virtual string ControlName
		{
			get
			{
				if (loadedControlName.Loader == null)
					loadedControlName.Loader = () => (string)ViewState["ControlName"];
				return string.IsNullOrEmpty(loadedControlName.Value) ? this.ClientID : loadedControlName.Value;
			}
			set
			{
				if (loadedControlName.Setter == null)
					loadedControlName.Setter = v => ViewState["ControlName"] = v;
				loadedControlName.Value = value;
			}
		}

		private LoadedValue<string> loadedPromptText;
		public virtual string PromptText
		{
			get
			{
				if (loadedPromptText.Loader == null)
					loadedPromptText.Loader = () => (string)ViewState["PromptText"];
				return string.IsNullOrEmpty(loadedPromptText.Value) ? Res.DropDownList_PromptText : loadedPromptText.Value;
			}
			set
			{
				if (loadedPromptText.Setter == null)
					loadedPromptText.Setter = v => ViewState["PromptText"] = v;
				loadedPromptText.Value = value;
			}
		}

		private LoadedValue<string> loadedDataTextField;
		public virtual string DataTextField
		{
			get
			{
				if (loadedDataTextField.Loader == null)
					loadedDataTextField.Loader = () => (string)ViewState["DataTextField"];
				return string.IsNullOrEmpty(loadedDataTextField.Value) ? "ModuleName" : loadedDataTextField.Value;
			}
			set
			{
				if (loadedDataTextField.Setter == null)
					loadedDataTextField.Setter = v => ViewState["DataTextField"] = v;
				loadedDataTextField.Value = value;
			}
		}

		private LoadedValue<string> loadedDataValueField;
		public virtual string DataValueField
		{
			get
			{
				if (loadedDataValueField.Loader == null)
					loadedDataValueField.Loader = () => (string)ViewState["DataValueField"];
				return string.IsNullOrEmpty(loadedDataValueField.Value) ? "ModuleCode" : loadedDataValueField.Value;
			}
			set
			{
				if (loadedDataValueField.Setter == null)
					loadedDataValueField.Setter = v => ViewState["DataValueField"] = v;
				loadedDataValueField.Value = value;
			}
		}

		private LoadedValue<string> loadedValue;
		public virtual string Value
		{
			get
			{
				if (loadedValue.Loader == null)
					loadedValue.Loader = () => (string)ViewState["Value"];
				return loadedValue.Value;
			}
			set
			{
				if (loadedValue.Setter == null)
					loadedValue.Setter = v => ViewState["Value"] = v;
				loadedValue.Value = value;
			}
		}

		private LoadedValue<int?> loadedModuleId;
		public int? ModuleId
		{
			get
			{
				if (loadedModuleId.Loader == null)
					loadedModuleId.Loader = () => SiteId.HasValue ? Modules.GetIdentifier(SiteId.Value, ModuleCode) : null;
				return loadedModuleId.Value;
			}
		}




		protected virtual string OptionItemAttachedAttributeHtml
		{
			get
			{
				string atts = "";
				Module.Module module = (Module.Module)Page.GetDataItem();
				string value = (string)Eval(DataValueField);
				if (module.ChildCount > 0)
					atts += @" style=""background-color:#ccc;""";
				//else
				//{
					atts += @" value=""" + value + @"""";
					if (value == Value)
						atts += @" selected=""selected""";
				//}
				return atts;
			}
		}

		private Stack<int> ParentIdStack = new Stack<int>();
		private int? AnteriorId = null;
		protected virtual string OptionText
		{
			get
			{
				string text = "";
				Module.Module module = (Module.Module)Page.GetDataItem();

				if (AnteriorId.HasValue && module.PModuleId == AnteriorId)
					ParentIdStack.Push(AnteriorId.Value);
				else
				{
					while (ParentIdStack.Count > 0 && (module.PModuleId == null || module.PModuleId.Value != ParentIdStack.Peek()))
						ParentIdStack.Pop();
				}
				for (int i = 0; i < ParentIdStack.Count; i++)
					text += "&nbsp;&nbsp;&nbsp;&nbsp;";
				text += module.ModuleName;

				AnteriorId = module.ModuleId;
				return text;
			}
		}



		protected virtual void CtrList_DataBinding(object sender, EventArgs e)
		{
			int? moduleId = null;
			if (!string.IsNullOrEmpty(ModuleCode))
				moduleId = ModuleId.Value;
			CtrList.DataSource = Modules.GetTreeListById(SiteId, moduleId);
		}
	}
}